package com.example.day_35.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.day_35.mapper.UserMapper;
import com.example.day_35.model.AjaxResult;
import com.example.day_35.model.User;
import com.example.day_35.service.IUserService;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("user")
@AllArgsConstructor
public class UserController {
    private final IUserService userService;
    private final UserMapper userMapper;


    @GetMapping("index")
    public String index(Model model){
        model.addAttribute("list",userService.list());
        return "user/index";
    }

    @PostMapping("index")
    @ResponseBody
    public AjaxResult<List<User>> index(){
        PageHelper.startPage(1,5);
        List<User> list = userService.list();
        return new AjaxResult<List<User>>(true,200,"成功",list);
    }

    @RequestMapping("list")
    @ResponseBody
    public List<User> list(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.like("name","%四");
        return userMapper.selectList(wrapper);
    }




    @GetMapping("/insert")
    public String insert(){
        return "user/add";
    }

    @PostMapping("/insert")
    @ResponseBody
    public AjaxResult insert(User user){
        userService.add(user);
        return new AjaxResult(true,200,"新增成功");
    }

    @GetMapping("/update")
    public String update(Integer id,Model model){
        model.addAttribute("id",id);
        return "user/update";
    }

    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(User user){
        userService.update(user);
        return new AjaxResult(true,200,"更新成功");
    }


    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(int id){
        userService.delete(id);
        AjaxResult result = new AjaxResult(true, 200, "删除成功");
        return result;
    }

    @GetMapping("/tologin")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(String name, String password, HttpSession session){
        User user =userService.login(name, password);
        if(user==null){
            return  AjaxResult.error(500,"账号密码不存在");
        }
        session.setAttribute("loginUser",user);
        return AjaxResult.success("登陆成功",null);
    }
    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        file.transferTo(new File("F:\\ad\\day_35\\src\\main\\resources\\static\\img"+File.separator+file.getOriginalFilename()));
        return AjaxResult.success("上传成功","");
    }

}





